<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\InvitationApplyLogic;
use app\cms\logic\SpeakerLogic;
use app\cms\model\Speaker;
use app\cms\validate\InvitationApplyValidate;
use Carbon\Carbon;
use Illuminate\Support\Facades\Event as IlluminateEvent;
use support\Request;
use support\Response;
use Workerman\Events\Event;

/**
 * 邀约申请控制器
 */
class InvitationApplyController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new InvitationApplyLogic();
        $this->validate = new InvitationApplyValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            // ['company_name', ''],
            ['name', ''],
            ['status', '']
        ]);
        // var_dump($where);
        $query = $this->logic->search($where);
        $query->with(['speaker' => function ($query) {
            $query->field('id,name,name_en,title');
        }]);
        if (!empty($where['name'])) {
            $query->whereOr('company_name', 'like', '%' . $where['name'] . '%')
                ->whereOr('phone', 'like', '%' . $where['name'] . '%')
                ->whereOr('email', 'like', '%' . $where['name'] . '%')
                ->whereOr('speaker_name', 'like', '%' . $where['name'] . '%');
        }

        // var_dump($query);

        $data = $this->logic->getList($query);

        return $this->success($data);
    }


    /**
     * 审核邀约申请信息
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-08
     * @param Request $request
     * @id 主键id
     * @audit_desc  审核意见
     * @status 状态
     * @return void
     */
    public function audit(Request $request)
    {
        $id = $request->input('id', '');
        $audit_desc = $request->input('audit_desc', '');
        $status = $request->input('status', 1);
        // ->update(['status' => $status, 'audit_desc' => $audit_desc, 'audit_time' => date('Y-m-d H:i:s', time())])
        $result = $this->logic->where('id', $id)->find();
        $speaker_id = $result['speaker_id'];
        $result->status = $status;
        $result->audit_desc = $audit_desc;
        $result->audit_time = date('Y-m-d H:i:s', time());
        $res = $result->save();
        if ($res) {
            if ($status == 1) {
                Speaker::where('id', $speaker_id)->inc('number_of_invitations')
                    ->update();
            }
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 查看审核意见
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-08
     * @param Request $request
     * @return void
     */
    public function checkAuditDesc(Request $request)
    {
        $id = $request->input('id', '');
        $result = $this->logic->where('id', $id)->field('id,audit_desc')->find();

        if ($result) {
            return $this->success($result, '操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
}
